﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Nano.Core
{
    public class Page
    {
        public int PageId { get; set; }
        public string Title { get; set; }
        public string Slug { get; set; }
        public string Description { get; set; }
        public string PageContent { get; set; }
        public string Keywords { get; set; }
        public DateTime DateCreated { get; set; }
        public DateTime? DateModified { get; set; }
        public bool IsPublished { get; set; }
        public bool IsFrontPage { get; set; }
        public bool ShowInSiteNavigation { get; set; }
        public int? ParentPageId { get; set; }
        public int? SortOrder { get; set; }

        #region Helpers

        /// <summary>
        /// Gets the homepage.
        /// </summary>
        /// <returns></returns>
        public static Page GetHomepage()
        {
            try 
	        {	        
		        using (var db = new DataContext())
                {
                    //  Front page must be published before anything can done.
                    return db.Pages.Single(p => p.IsFrontPage && p.IsPublished);
                }
	        }
	        catch (Exception)
	        {
		        return null;
	        }
        }

        /// <summary>
        /// Gets a single page by id.
        /// </summary>
        /// <param name="pageId">The page id.</param>
        /// <returns></returns>
        public static Page GetPage(int pageId)
        {
            try
            {
                using (var db = new DataContext())
                {
                    //  Return a single page by id.
                    return db.Pages.Single(p => p.PageId == pageId && p.IsPublished);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        #endregion
    }
}